# -*- coding: utf-8 -*-
"""
    201901, Dr. Jie Zheng, Beijing & Xinglong, NAOC
    202101-? Dr. Jie Zheng & Dr./Prof. Linqiao Jiang
    Light_Curve_Pipeline
    v3 (2021A) Upgrade from former version, remove unused code
"""


import numpy as np
import astropy.io.fits as fits
import os
from .JZ_utils import loadlist, datestr, logfile, conf, meanclip


def _se_(ini, scif, sef, catf, txtf, phot_aper, skiptag, lf):
    """
    Photometric working, this function is calling Source-Extractor,
    but other implementation can call pure python solution
    :param ini:
    :param scif:
    :param sef:
    :param catf:
    :param txtf:
    :param phot_aper: aperture of photometry, 20221008
                        >0.1 for pixels, 
                        <-0.1 for times of fwhm, 
                        -0.1~0.1 for mag_auto
    :param skiptag:
    :param lf:
    :return:
    """

    nf = len(scif)
    lf.show("{:03d} files".format(nf), logfile.DEBUG)

    # choose default sex config from working dir or program dir
    if os.path.isfile("default.sex"):
        def_sex = "default.sex"
    else:
        def_sex = os.path.split(__file__)[0] + "/default.sex"
    def_par = os.path.split(__file__)[0] + "/default.param"
    
    # 20221008 real-aper
    rap = phot_aper if phot_aper > 0.1 else 10  # if pixels provided

    se_cmd_fmt1 = "{se} -c {st} -parameters_name {par} {{img}} -CATALOG_NAME {{se}} -PHOT_APERTURES {ap:.2f}".format(
        se=ini["se_cmd"], st=def_sex, par=def_par, ap=rap)
    # 20221008 add cmd2, if use times of fwhm
    se_cmd_fmt2 = "{se} -c {st} -parameters_name {par} {{img}} -CATALOG_NAME {{se}} -PHOT_APERTURES {{ap:.2f}}".format(
        se=ini["se_cmd"], st=def_sex, par=def_par)
    # se_cmd_fmt1 = ini["se_cmd"] + " -c " + def_sex + " -parameters_name " + def_par + " {img} -CATALOG_NAME {cat}"

    # load images and process
    for f in range(nf):
        if skiptag[f]:
            lf.show("SKIP: \"" + catf[f] + "\"", logfile.DEBUG )
            continue
        lf.show("SE on {:03d}/{:03d}: {:40s}".format(f + 1, nf, scif[f]), logfile.DEBUG)

        # first, use auto
        se_cmd = se_cmd_fmt1.format(img=scif[f], se=sef[f])
        # following execution canceled, because real aperture is not so nice
        lf.show("    " + se_cmd, logfile.DEBUG)
        os.system(se_cmd)

        # 210329 generate the new catalog with LCP format from SE output
        secat = fits.getdata(sef[f], 2)
        ns = len(secat)
        mycat = np.empty(ns, [
            ("Num",   np.uint16 ),
            ("X",     np.float64),
            ("Y",     np.float64),
            ("Elong", np.float32),
            ("FWHM",  np.float32),
            ("Mag",   np.float32),
            ("Err",   np.float32),
            ("Flags", np.uint16 ),
            ("Alpha", np.float64),
            ("Delta", np.float64),
        ])
        mycat["Num"  ] = secat["NUMBER"]
        mycat["X"    ] = secat[ini["se_x"]]
        mycat["Y"    ] = secat[ini["se_y"]]
        mycat["Elong"] = secat[ini["elong"]]
        mycat["FWHM" ] = secat[ini["fwhm"]]
        # use auto as default
        mycat["Mag"  ] = secat["MAG_AUTO"]
        mycat["Err"  ] = secat["MAGERR_AUTO"]
        mycat["Flags"] = secat["FLAGS"]
        mycat["Alpha"] = secat[ini["se_ra"]]
        mycat["Delta"] = secat[ini["se_de"]]

        hdr = fits.getheader(scif[f])
        hdr["IMNAXIS1"] = hdr["NAXIS1"]
        hdr["IMNAXIS2"] = hdr["NAXIS2"]
        hdr["APERTURE"] = phot_aper
        
        # 20221008
        if phot_aper > 0.1:
            # if fixed aperture photometry is required
            mycat["Mag"  ] = secat["MAG_APER"]
            mycat["Err"  ] = secat["MAGERR_APER"]
        elif phot_aper < -0.1:
            # if fwhm aperture photometry is required
            # compute mean fwhm of all good stars
            fwhm_med, fwhm_std = meanclip(mycat["FWHM"][mycat["Err"]<0.1])
            lf.show("      FWHM = {:.2f} +- {:.2f}".format(fwhm_med, fwhm_std), logfile.DEBUG)
            # get the real aperture
            rap = - phot_aper * fwhm_med
            # run se again and load new result
            se_cmd = se_cmd_fmt2.format(img=scif[f], se=sef[f], ap=rap)
            lf.show("    " + se_cmd, logfile.DEBUG)
            os.system(se_cmd)
            secat = fits.getdata(sef[f], 2)
            mycat["Mag"  ] = secat["MAG_APER"]
            mycat["Err"  ] = secat["MAGERR_APER"]

        pri_hdu = fits.PrimaryHDU(header=hdr)
        tb_hdu = fits.BinTableHDU(data=mycat)
        new_fits = fits.HDUList([pri_hdu, tb_hdu])
        new_fits.writeto(catf[f], overwrite=True)
        lf.show("SE result transfer to {}".format(catf[f]), logfile.DEBUG)

        with open(txtf[f], "w") as ff:
            ff.write("Num   X        Y         Elong FWHM   Mag    Err    Flags             Alpha      Delta\n")
            for s in mycat:
                    ff.write("{s[Num]:4d}  {s[X]:8.3f} {s[Y]:8.3f}  {s[Elong]:5.2f} {s[FWHM]:5.2f}  "
                             "{s[Mag]:6.3f} {s[Err]:6.4f}  {s[Flags]:16b}  {s[Alpha]:10.6f} {s[Delta]:+10.6f}\n".format(
                            s=s))
        lf.show("SE result transfer to text: {}".format(txtf[f]), logfile.DEBUG)

    lf.show("Photometry on {} of {} files".format(nf - sum(skiptag), nf), logfile.INFO)